package com.zlq.iwork.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.zlq.iwork.R;
import com.zlq.iwork.adapter.WorkListAdapter;
import com.zlq.iwork.bean.Note;
import com.zlq.iwork.bean.NoteDao;

public class MainActivity extends BaseActivity implements OnClickListener {
	ImageButton title_work_add;
	ImageButton title_work_setting;
	ListView work_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findView();
		fillList();
	}

	private void fillList() {
		List<Note> nList=new NoteDao(this).findAll();
	
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (Note note:nList) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("name", note.getUserid());
			map.put("date", note.getDate());
			map.put("note", note.getNote());
			list.add(map);
		}
		
		WorkListAdapter adapter=new WorkListAdapter(this, list);
		work_list.setAdapter(adapter);
	}

	public void findView() {
		title_work_add = (ImageButton) findViewById(R.id.title_wrok_add);
		title_work_setting = (ImageButton) findViewById(R.id.title_wrok_setting);
		title_work_add.setOnClickListener(this);
		title_work_setting.setOnClickListener(this);
		work_list = (ListView) findViewById(R.id.list_works);
	}

	public void onClick(View v) {
		int vId = v.getId();
		Intent intent = null;
		switch (vId) {
		case R.id.title_wrok_add:
			intent = new Intent(this, WorkAddActivity.class);
			startActivityForResult(intent, 0);
			break;
		case R.id.title_wrok_setting:
			intent = new Intent(this, Setting.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (resultCode) {
		case RESULT_OK:
			fillList();
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
